package leetcode.editor.day;

import java.util.HashMap;
import java.util.Map;

// 1460. 通过翻转子数组使两个数组相等
// https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/
class MakeTwoArraysEqualByReversingSubArrays {
    public static void main(String[] args) {
        Solution solution = new MakeTwoArraysEqualByReversingSubArrays().new Solution();
        solution.canBeEqual(new int[]{1, 2, 3}, new int[]{2, 2, 2});
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        // hash表，另一种方法，排序比较元素是否相同
        // https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/solution/tong-guo-fan-zhuan-zi-shu-zu-shi-liang-g-dueo/
        public boolean canBeEqual(int[] target, int[] arr) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < target.length; i++) {
                map.put(target[i], map.getOrDefault(target[i], 0) + 1);
            }

            for (int i = 0; i < arr.length; i++) {
                if (map.containsKey(arr[i]) && map.get(arr[i]) > 0) {
                    map.put(arr[i], map.getOrDefault(arr[i], 0) - 1);
                } else return false;
            }

            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
